package cn.zxcoder.controller.SystemManage.variable;

import cn.zxcoder.entity.Res;
import cn.zxcoder.entity.SystemManage.Variable;
import cn.zxcoder.entity.login.Account;
import cn.zxcoder.service.IVariableService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Date;
import java.util.List;
import java.util.jar.JarEntry;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zx
 * @since 2023-05-12
 */
@RestController
@Api(tags = "系统环境变量的控制器")
@RequestMapping("/api/Variable")
public class VariableController {

    @Autowired
    IVariableService iVariableServiceIml;

    @ApiOperation("获取系统环境变量列表")
    @PostMapping("/list")
    @Transactional(readOnly = true)
    public Res getVariable(@RequestParam String page, @RequestParam String limit, @RequestParam String varDescription,
                           @RequestParam String varName) {

        Page<Variable> variablePage = new Page<>(Integer.parseInt(page), Integer.parseInt(limit));
        QueryWrapper<Variable> wrapper = new QueryWrapper<>();
        if (!varDescription.equals("")) {
            wrapper.like("var_description", varDescription);
        }
        if (!varName.equals("")) {
            wrapper.like("var_name", varName);
        }
        Page<Variable> page1 = iVariableServiceIml.page(variablePage, wrapper);
        if (page1.getRecords().size() != 0) {
            return new Res(true, JSON.toJSONString(page1), "获取系统环境变量成功");
        } else {
            return new Res(true, JSON.toJSONString(page1), "暂时还没有设置系统环境变量哦");

        }

    }

    @ApiOperation("保存系统环境变量")
    @PostMapping("/save")
    @Transactional

    public Res saveVariable(@RequestParam String varPath, @RequestParam String varDescription,@RequestParam String varId,
                           @RequestParam String varName, @RequestParam String token,@RequestParam boolean issave) {

        Variable variable = new Variable();
        variable.setEditTime(new Date());
        variable.setEditUser(token);
        variable.setVarDescription(varDescription);
        variable.setVarName(varName);
        variable.setVarPath(varPath);

        if (issave) {//保存
            boolean save = iVariableServiceIml.save(variable);
            return new Res(save, null, save ? "保存环境变量成功" : "保存环境变量失败");
        } else {//编辑
            UpdateWrapper<Variable> wrapper = new UpdateWrapper<>();
            wrapper.eq("id", Integer.parseInt(varId));
            boolean update = iVariableServiceIml.update(variable,wrapper);
            return new Res(update, null, update ? "编辑环境变量成功" : "编辑环境变量失败");

        }

    }


    @ApiOperation("删除系统环境变量")
    @DeleteMapping("/delete/{id}")
    @Transactional

    public Res deleteleteVariable(@PathVariable String id) {

        boolean delete = iVariableServiceIml.removeById(Integer.parseInt(id));
        return new Res(delete, null, delete ? "删除环境变量成功" : "删除环境变量失败");
    }



}
